package uiLayer;

import java.util.NoSuchElementException;
import java.util.Scanner;



import controllerLayer.ProductCtr;

public class ProductUI {
	
	private ProductCtr pctr;
	private static ProductUI instance = null;;

	public ProductUI() {
		pctr = new ProductCtr();
	}
	public static ProductUI getInstance()       
	{
		if(instance == null)
		{
			instance = new ProductUI();
		}
		return instance;
	}
	public void productMenuStart() {
		boolean exit = false;
		while(!exit){
		int choice = printProductMenu();
		switch(choice)
		{
		 case 1: createProduct();
         break;
		 case 2: createCollection();
		 break;
		 case 3: addToCollection();
		 break;
		 case 4: findByID();
         break;
		 case 5: updateProduct();
         break;
		 case 6: deleteProduct();
		 break;
		 case 7: printLabel();
		 break;
		 case 8: goBack();
         break;
		}
		}
	}


	private void printLabel() {
		Scanner keyboard = new Scanner(System.in); 
		System.out.println("Product ID");
		String id = keyboard.nextLine();
		try{
			pctr.getItem(id).toString();
		}
		catch(NullPointerException npe){
			System.out.println("Product doesn't exist");
		
		}
		
	}
	private void addToCollection() {
		
		Scanner keyboard = new Scanner(System.in); 
		System.out.println("Product ID");
		int id = InputParser.getInstance().parseInt(keyboard.nextLine());
		System.out.println("Collection ID");
		String ID = keyboard.nextLine();
		
		pctr.addToSLI(id, ID);
		
		
	}
	private void createCollection() {
		Scanner keyboard = new Scanner(System.in); 
		System.out.println("Collection name:");
		String name = keyboard.nextLine();
		
		System.out.println("Collection ID: "+pctr.createCollection(name));
		
		
		
	}
	private void deleteProduct() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Delete Product***");
        System.out.println("ID: ");
        String ID = keyboard.nextLine();
        
        pctr.deleteItem(ID);
		
	}

	private void goBack() {
		OfficeUI.getInstance().run();
		
	}

	private void updateProduct() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Update Product***");
        System.out.println("ID: ");
        String id = keyboard.nextLine();
        System.out.println("Price: ");
        double price = InputParser.getInstance().parseDouble(keyboard.nextLine());
        System.out.println("Quantity of new ordered products: ");
        double quantity = InputParser.getInstance().parseDouble(keyboard.nextLine());
        if(quantity!=0)System.out.println("Contractor ID: ");
        String contID = keyboard.nextLine();
        System.out.println("Location: ");
        String location = keyboard.nextLine();
        System.out.println("Alert message: ");
        double alert = InputParser.getInstance().parseDouble(keyboard.nextLine());
		pctr.updateItem(id,contID,quantity,location,price,alert);
		
	}

	private void findByID() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Find Product***");
        System.out.println("ID: ");
        int ID = InputParser.getInstance().parseInt(keyboard.nextLine());
        
        System.out.println( pctr.findItem(ID));
		
	}

	private void createProduct() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Create Product***");
        System.out.println("Name: ");
        String name = keyboard.nextLine();
        System.out.println("Description: ");
        String description = keyboard.nextLine();
        System.out.println("Category: ");
        String category = keyboard.nextLine();
        System.out.println("Price: ");
        double price = InputParser.getInstance().parseDouble(keyboard.nextLine());
        System.out.println("Quantity: ");
        double quantity = InputParser.getInstance().parseDouble(keyboard.nextLine());
        System.out.println("Measure: ");
        String measure = keyboard.nextLine();
        System.out.println("Location: ");
        String location = keyboard.nextLine();
        System.out.println("When alert that product is running out: ");
        double alert = InputParser.getInstance().parseDouble(keyboard.nextLine());
        System.out.println("Contractor ID: ");
        String cID = keyboard.nextLine();
  
		pctr.createItem(cID, measure, name,description,price,quantity,category,location,alert);
		
	}

	private int printProductMenu() {
		Scanner keyboard = new Scanner(System.in);
	        System.out.print('\f');
	        System.out.println("*** Product Menu ***");
	        System.out.println("1. Create Product");
	        System.out.println("2. Create Collection");
	        System.out.println("3. Add Product to Collection");
	        System.out.println("4. Find Product");
	        System.out.println("5. Update Product");
	        System.out.println("6. Delete Product");
	        System.out.println("7. Print Label");
	        System.out.println("8. Quit");
	        System.out.println("Your choice #: ");
	        
	        int choice = InputParser.getInstance().parseInt(keyboard.nextLine());
	        return choice;
	}


}
